package com.erp.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.sys.Service.LoginInfoService;
import com.erp.sys.Service.UserService;
import com.erp.sys.Util.WebUtil;
import com.erp.bus.comm.ResultCode;
import com.erp.sys.pojo.LoginInfo;
import com.erp.sys.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private LoginInfoService loginInfoService;

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public ResultCode login(String loginname, String pwd ){

        // 添加一条登录信息
        LoginInfo logininfo =  new LoginInfo();
        logininfo.setLoginname(loginname);

        // 获取请求ip地址
        //  request
        logininfo.setLoginip(WebUtil.getRequest().getRemoteAddr());
        logininfo.setLogintime(new Date());
        loginInfoService.save(logininfo);

        //loginname和pwd进行查询
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("loginname",loginname).eq("pwd",pwd);
        User user = userService.getOne(qw);
        if (user != null){
            WebUtil.getSession().setAttribute("user",user);
            // 登录成功
            return  ResultCode.SUCCESS_LOGIN;
        }else{
            return  ResultCode.FAIL_LOGIN;
        }
    }
}
